﻿namespace OleDbDAL.Comm
{
    using DBUtility;
    using IDal.Comm;
    using Model.Comm;
    using System;
    using System.Data;
    using System.Text;

    public class CommApp : IComm
    {
        private string dbname = "Systems";

        public void Add(CommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Comm(");
            builder.Append("CommTypeId,ChainNum,CommNum,CommName,CommCode,CommUnit,CommSpec,DefaultPrice,MinDiscount,IsAllowDiscount,CommBarCode,CommDefaultDlId,lastUpdateTime,OrderNum");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.CommTypeId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.CommNum + "',");
            builder.Append("'" + model.CommName + "',");
            builder.Append("'" + model.CommCode + "',");
            builder.Append("'" + model.CommUnit + "',");
            builder.Append("'" + model.CommSpec + "',");
            builder.Append("" + model.DefaultPrice + ",");
            builder.Append("" + model.MinDiscount + ",");
            builder.Append("" + model.IsAllowDiscount + ",");
            builder.Append("'" + model.CommBarCode + "',");
            builder.Append("" + model.CommDefaultDlId + ",");
            builder.Append("'" + model.lastUpdateTime + "',");
            builder.Append("" + model.OrderNum + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int CommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Comm ");
            builder.Append(" where CommId=" + CommId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Comm ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CommId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "CommId", "T_Comm", swhere);
        }

        public CommEnt GetModel(int CommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CommId,CommTypeId,ChainNum,CommNum,CommName,CommCode,CommUnit,CommSpec,DefaultPrice,MinDiscount,IsAllowDiscount,CommBarCode,CommDefaultDlId,lastUpdateTime,OrderNum from T_Comm ");
            builder.Append(" where CommId=" + CommId);
            CommEnt ent = new CommEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.CommId = CommId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["CommTypeId"].ToString() != "")
                {
                    ent.CommTypeId = int.Parse(table.Rows[0]["CommTypeId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.CommNum = table.Rows[0]["CommNum"].ToString();
                ent.CommName = table.Rows[0]["CommName"].ToString();
                ent.CommCode = table.Rows[0]["CommCode"].ToString();
                ent.CommUnit = table.Rows[0]["CommUnit"].ToString();
                ent.CommSpec = table.Rows[0]["CommSpec"].ToString();
                if (table.Rows[0]["DefaultPrice"].ToString() != "")
                {
                    ent.DefaultPrice = decimal.Parse(table.Rows[0]["DefaultPrice"].ToString());
                }
                if (table.Rows[0]["MinDiscount"].ToString() != "")
                {
                    ent.MinDiscount = decimal.Parse(table.Rows[0]["MinDiscount"].ToString());
                }
                if (table.Rows[0]["IsAllowDiscount"].ToString() != "")
                {
                    ent.IsAllowDiscount = int.Parse(table.Rows[0]["IsAllowDiscount"].ToString());
                }
                ent.CommBarCode = table.Rows[0]["CommBarCode"].ToString();
                if (table.Rows[0]["CommDefaultDlId"].ToString() != "")
                {
                    ent.CommDefaultDlId = int.Parse(table.Rows[0]["CommDefaultDlId"].ToString());
                }
                if (table.Rows[0]["lastUpdateTime"].ToString() != "")
                {
                    ent.lastUpdateTime = DateTime.Parse(table.Rows[0]["lastUpdateTime"].ToString());
                }
                if (table.Rows[0]["OrderNum"].ToString() != "")
                {
                    ent.OrderNum = int.Parse(table.Rows[0]["OrderNum"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Comm ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "CommId");
        }

        public void Update(CommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Comm set ");
            builder.Append("CommTypeId=" + model.CommTypeId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("CommNum='" + model.CommNum + "',");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("CommCode='" + model.CommCode + "',");
            builder.Append("CommUnit='" + model.CommUnit + "',");
            builder.Append("CommSpec='" + model.CommSpec + "',");
            builder.Append("DefaultPrice=" + model.DefaultPrice + ",");
            builder.Append("MinDiscount=" + model.MinDiscount + ",");
            builder.Append("IsAllowDiscount=" + model.IsAllowDiscount + ",");
            builder.Append("CommBarCode='" + model.CommBarCode + "',");
            builder.Append("CommDefaultDlId=" + model.CommDefaultDlId + ",");
            builder.Append("lastUpdateTime='" + model.lastUpdateTime + "',");
            builder.Append("OrderNum=" + model.OrderNum + "");
            builder.Append(" where CommId=" + model.CommId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

